home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: Tradition or what?
- Date: Sat, 16 Mar 1996 14:26:49 GMT
- Organization: Netcom
- Message-ID: <314ac1ef.49220064@nntp.ix.netcom.com>
- References: <4g0elg$mdr@redstone.interpath.net> <4hpd8a$d70@alterdial.UU.NET> <1996Mar8.153250.115645@kuhub.cc.ukans.edu>
- NNTP-Posting-Host: ix-dc8-04.ix.netcom.com
- X-NETCOM-Date: Sat Mar 16 6:27:11 AM PST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- anh@kuhub.cc.ukans.edu wrote:
-
- > Well, I found one good use of magic numbers such as when one needs a
- > localized temporary buffer of data.
- >
- > int func()
- > {
- > FILE *fp;
- > char buf[15+1];
- >
- > ...
- >
- > fgets(buf,15,fp);
- > }
- >
- >
- > Well, if I know the data is always going to be less than 15 or whatever,
- > there is really no need to use a #define here.
-
- Non sequitur. Whether you know that the data is going to be less than
- 15 is immaterial. In this situration I'd very rarely use a #define
- for the size of the buffer, but I would absolutely never repeat the
- size as a literal later in the code. My preference is something like
-
- int func()
- {
- FILE *fp;
- char buf[15];
-
- ...
-
- fgets(buf, sizeof buf, fp);
- }
-
- Note that I've changed the size of the buffer to 15 on the assumption
- that the intention is that the size of the data be at most 14
- characters excluding the terminating nul.
-
- Michael M Rubenstein
-